package com.dingding.service.systemservice.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.bean.databean.request.ReqCheckUpdate;
import com.dingding.bean.databean.request.ReqGetSysMessage;
import com.dingding.bean.databean.response.DataResponse;
import com.dingding.bean.databean.response.ResCheckUpdate;
import com.dingding.bean.databean.response.ResGetSysMessage;
import com.dingding.bean.databean.response.ResServiceSystemTime;
import com.dingding.bean.entity.UserLogin;
import com.dingding.common.core.config.SystemCode;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.credential.GeneraterClinetSequence;
import com.dingding.dao.sql.SystemNewsServiceSQL;
import com.dingding.service.BaseService;
import com.dingding.service.systemservice.ISystemNewsService;

/**
 * 
 * @ClassName: SystemNewsServiceImpl
 * @Description: 系统消息Service实现类
 * @author zhaochenguang
 * @date 2015-10-22 上午11:34:04
 * @version 叮叮搭车V2.0
 */
@SuppressWarnings("all")
public class SystemNewsServiceImpl extends BaseService implements
		ISystemNewsService {

	// 日志
	private Logger log = Logger.getLogger(SystemNewsServiceImpl.class);

	/**
	 * 
	 * @Title: getSysMessage
	 * @Description: 获取系统消息
	 * @param reqGetSysMessage
	 *            获取系统消息请求bean
	 * @param dataResponse
	 *            响应信息封装
	 * @author zhaochenguang
	 * @return void
	 * @throws
	 * @Override
	 */

	@Transactional(readOnly = true)
	public void getSysMessage(ReqGetSysMessage reqGetSysMessage,
			DataResponse dataResponse) {

		// 验证获取系统消息时客户端传入参数是否为空
		boolean flag = this.validateGetSysMessage(reqGetSysMessage);

		if (!flag) {

			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		List<Object> resGetSysMessages = null;

		try {
			// 分页查询系统信息
			resGetSysMessages = super.commonDao.hqlQueryListByPage(
					SystemNewsServiceSQL.GET_SYSTEM_MESSAGE,
					reqGetSysMessage.getPageIndex(),
					reqGetSysMessage.getPageSize(), false);

		} catch (Exception e) {
			log.error("获取系统信息出现异常！SystemNewsServiceImpl 方法:getSysMessage()", e);
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}

		// 转化数据库查询结果为响应封装bean
		List<ResGetSysMessage> getSysMessages = (List<ResGetSysMessage>) (List) resGetSysMessages;

		// 设置响应信息
		setStatus(dataResponse, SystemCode.SUCCESS);
		dataResponse.setResponseBody(getSysMessages);

	}

	/**
	 * @Title: validateGetSysMessage
	 * @Description: 验证获取系统消息时传入的参数是否为空
	 * @param reqGetSysMessage
	 *            客户端传入参数
	 * @return boolean 返回类型
	 * @throws
	 */
	public boolean validateGetSysMessage(ReqGetSysMessage reqGetSysMessage) {

		if (reqGetSysMessage == null // 获取系统消息传入的参数是否为空
				|| reqGetSysMessage.getPageIndex() == 0 // 当前是否为空
				|| reqGetSysMessage.getPageSize() == 0) { // 每页显示记录数是否为空

			return false;
		}

		return true;
	}

	/**
	 * 
	 * @Title: checkUpdate
	 * @Description: 获取更新
	 * @param reqCheckUpdate
	 * @param dataResponse
	 * @author lijinchao
	 * @return void
	 * @throws
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void checkUpdate(ReqCheckUpdate reqCheckUpdate,
			DataResponse dataResponse) {

		// 验证检查更新的请求参数是否为空
		boolean flag = this.validateReqCheckUpdate(reqCheckUpdate);

		if (!flag) {

			setStatus(dataResponse, SystemCode.DATA_NULL_ERROR);
			return;
		}

		// 获取响应设备类型的更新内容
		ResCheckUpdate resCheckUpdate = super.systemNewsDao
				.getUpdateInfo(reqCheckUpdate.getDeviceType());

		// 生成并保存appid
		boolean isSuccess = GeneraterClinetSequence.generaterAppId(reqCheckUpdate, resCheckUpdate);
		
		// 出现错误
		if (!isSuccess) {
			setStatus(dataResponse, SystemCode.ERROR);
			return;
		}
		
		//更新用户最新版本
		if(ToolUtil.isNotBlank(reqCheckUpdate.getUserId()) && ToolUtil.isNotBlank(reqCheckUpdate.getClientVersion())){
			
			//根据用户Id获取用户登录信息
			UserLogin userLogin = null;
			try {
				userLogin = super.userLoginDao.getById(reqCheckUpdate.getUserId(), false);
			} catch (Exception e) {
				log.error("版本更新，该用户不存在！！！");
			}
			
			//判断该用户的登录信息是否为空
			if(userLogin != null){
				//更新用户APP版本号
				userLogin.setClientVersion(reqCheckUpdate.getClientVersion());
			}
			
		}
		
		setStatus(dataResponse, SystemCode.SUCCESS);
		dataResponse.setResponseBody(resCheckUpdate);
	}

	/**
	 * @Title: validateReqCheckUpdate
	 * @Description: 检查更新的请求bean
	 * @param reqCheckUpdate
	 * @author lijinchao
	 * @return boolean
	 * @throws
	 */
	private boolean validateReqCheckUpdate(ReqCheckUpdate reqCheckUpdate) {

		// 判断是否有设备类型
		if (reqCheckUpdate == null || reqCheckUpdate.getDeviceType() == null) {

			return false;
		}

		return true;
	}

	@Override
	public void setStatus(DataResponse dataResponse, int status) {
		switch (status) {
		// 成功
		case SystemCode.SUCCESS:
			dataResponse.setResponseCode(SystemCode.SUCCESS);
			dataResponse.setResponseMessage(SystemCode.SUCCESS_MESSAGE);
			break;
		// 错误
		case SystemCode.ERROR:
			dataResponse.setResponseCode(SystemCode.ERROR);
			dataResponse.setResponseMessage(SystemCode.ERROR_MESSAGE);
			break;
		// 客户端传递的参数为空
		case SystemCode.DATA_NULL_ERROR:
			dataResponse.setResponseCode(SystemCode.DATA_NULL_ERROR);
			dataResponse.setResponseMessage(SystemCode.DATA_NULL_MESSAGE);
			break;

		// 默认
		default:
			dataResponse.setResponseCode(SystemCode.ERROR);
			dataResponse.setResponseMessage(SystemCode.ERROR_MESSAGE);
			break;
		}

	}

	/**
	 * 获取系统时间
	 */
	@Override
	public void getServiceSystemTime(DataResponse dataResponse) {

		// 获取系统时间响应
		ResServiceSystemTime resGetOrderOutTime = new ResServiceSystemTime(new Date());
		
		setStatus(dataResponse, SystemCode.SUCCESS);
		dataResponse.setResponseBody(resGetOrderOutTime);
	}

}
